package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.common.Result;
//import com.example.demo.
import com.example.demo.entity.User;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserMapper userMapper;

    @PostMapping("/login")//接口写好了,前台传递数据。
    public Result<?> login(@RequestBody User user) {
        //映射成对象实体
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", user.getUsername());
        queryWrapper.eq("password", user.getPassword());
        User res = userMapper.selectOne(queryWrapper);
        if (res == null) {
            return Result.error("-1", "登录用户名或密码错误");
        }

        return Result.success(res);
    }


    @PostMapping("/register")//接口写好了,前台传递数据。
    public Result<?> register(@RequestBody User user) {
        System.out.println("TEST: "+user);
        userMapper.insert(user);
        return Result.success();
    }


    //("/add")
    @PostMapping//接口写好了,前台传递数据。
    public Result<?> save(@RequestBody User user) {
        //映射成对象实体
        if (user.getPassword() == null) {
            user.setPassword("123456");
        }
        userMapper.insert(user);//插入数据库
        System.out.println("TEST: "+user);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody User user) {
        //映射成对象实体

        userMapper.updateById(user);
        //修改数据库
        return Result.success();
    }



    @DeleteMapping("/{id}")
    public Result<?> update(@PathVariable Long id) {
        //映射成对象实体
        userMapper.deleteById(id);
        //修改数据库
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result<?> getById(@PathVariable Long id) {
        return Result.success(userMapper.selectById(id));
    }

   /* @GetMapping("/{address}")
    public Result<?> showSelected(@RequestParam String stuId){

    }
*/

    /*
        // 展示已选课程
  @GetMapping("showSelected")
  public Result<?> showSelected(@RequestParam String stuId) {
    LambdaQueryWrapper<StuCourse> queryWrapper =
        Wrappers.<StuCourse>lambdaQuery()
            .eq(StuCourse::getStuId, stuId)
            .orderByDesc(StuCourse::getId);
    List<StuCourse> allCourses = stuCourseMapper.selectList(queryWrapper);
    for (int i = 0; i < allCourses.size(); i++) {
      if (Objects.equals(allCourses.get(i).getTsScore(), 0)) {
        StuCourse temp = allCourses.get(i);
        temp.setScore(0);
        allCourses.set(i, temp);
      }
    }
*/
    //("/selectAllUsers")
    @GetMapping//接口写好了,前台传递数据。
    // @ResponseBody
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
                              @RequestParam(defaultValue = "10") Integer pageSize,
                              @RequestParam(defaultValue = "") String search) {

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(search)) {
            queryWrapper.like("username", search);
        }

        queryWrapper.orderByDesc("address");
        System.out.println(search);
        Page<User> userPage = userMapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        List<User> userList = userPage.getRecords();
        for (int i = 0; i < userList.size(); i++) {
            userList.get(i).setRank((i+1)+pageSize*(pageNum-1));
            if(Objects.equals(userList.get(i).getGpa(),"1"))
            {
                User temp=userList.get(i);
                temp.setAddress(null);
                userList.set(i,temp);
            }
        }
        userPage.setRecords(userList);
        return Result.success(userPage);
    }
}

//映射成对象实体
//new Page<>(pageNum,pageSize);
// Page<Uesr> usrPage= userMapper.selectPage(new Page<>(pageNum,pageSize), Wrappers.<Uesr>lambdaQuery().like(Uesr::getUsername,search));//分页模糊查询

// userMapp